<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');

//用户控制器
class User extends Home_Controller{

	public function __construct(){
		parent::__construct();
		$this->load->model('user_model');
        $this->load->model('orders_model');
        //加载分页
        $this->load->library('pagination');
        //$this->output->enable_profiler(true);
	}
	#显示注册页面
	public function register(){
		$this->load->view('register.html');
	}

	#显示登录页面
	public function login(){
		$this->load->view('login.html');
	}

	public function do_register(){
        $data['user_phone'] = $this->input->post('user_phone');
        $password = $this->input->post('password');
        $data['password'] = md5($password);
        $data['reg_time'] = time();
        if ($this->user_model->add_user($data)) {
            $mess = array();
            # 注册成功
            $mess['stat'] = 1;
            $mess['message'] = '注册成功';
            $mess['url'] = base_url().'user/login';
            echo json_encode($mess);
            exit();
        } else {
            $mess = array();
            # 注册失败
            $mess['stat'] = 0;
            $mess['message'] = '注册失败';
            echo json_encode($mess);
            exit();
        }

	}


	#登录动作
	public function signin(){
		$user_phone = $this->input->post('user_phone');
		$password = $this->input->post('password');

		if ($user = $this->user_model->get_user($user_phone,$password)) {
			#成功，将用户信息保存至session
			$this->session->set_userdata('user',$user);
            //记住密码
            $remember=$this->input->post('remember');
            //如果用户选择了，记录登录状态就把用户名和加了密的密码放到cookie里面
            if(!empty($remember)){
                setcookie("username", $user_phone, time()+3600*24*7);
                setcookie("password",$password, time()+3600*24*7);

            }else{
                if(!empty($_COOKIE['username']) || !empty($_COOKIE['password'])){
                    setcookie("username", null, time()-3600*24*7);
                    setcookie("password", null, time()-3600*24*7);
                }
            }
            $mess = array();
            $mess['stat'] = 1;
            $mess['message'] = '登录成功';
            $mess['url'] = base_url().'user/center';
            echo json_encode($mess);
            exit();

		} else {
			# error
            $mess = array();
            $mess['stat'] = 0;
            $mess['message'] = '登录失败';
            $mess['url'] = base_url();
            echo json_encode($mess);
            exit();
		}
		
	}

	//用户中心
    public function center(){
        //看session是否存在当前用户user_id，不存在就让其登录
        //当前用户user_id
        $user=$this->session->userdata('user');
        if(empty($user)){
            //登录
            redirect('user/login');
        }
        $user_id = $user['user_id'];
        $data['status'] = $pay_status = $this->input->get("status");
        //CI框架自带分页开始
        if(isset($pay_status)){
            $config['base_url'] = base_url()."user/center?status={$pay_status}";
        }else{
            $config['base_url'] = base_url()."user/center";
        }
        
        //每页显示的条数
        $config['per_page']=$limit=1;
        //放在你当前页码的前面和后面的“数字”链接的数量。比方说值为 2 就会在每一边放置 2 个数字链接，就像此页顶端的示例链接那样。
        $config['num_links'] = 4;
        $config['uri_segment'] = 3;
        $config['cur_tag_open'] = '<li class="am-active"><a href="#">';
        $config['cur_tag_close'] = '</a></li>';
        $config['prev_tag_open'] = '<li>';
        $config['prev_tag_close'] = '</li>';
        $config['next_tag_open'] = '<li>';
        $config['next_tag_close'] = '</li>';
        $config['num_tag_open'] = '<li>';
        $config['num_tag_close'] =  '</li>';
        $config['use_page_numbers'] = true;//默认分页URL中是显示每页记录数,启用use_page_numbers后显示的是当前页码，
        $config['first_link'] = '首页'; //你希望在分页的左边显示“第一页”链你不希接的名字。如果望显示，可以把它的值设为 FALSE 。
        $config['last_link'] = '尾页';//你希望在分页的右边显示“最后一页”链接的名字。如果你不希望显示，可以把它的值设为 FALSE 。
        $config['prev_link'] = '上一页';
        $config['next_link'] = '下一页';
        $config['page_query_string'] = true;
        //分页的总条数
        $config['total_rows']=$this->orders_model->count_user_order($user_id,$pay_status);
        $this->pagination->initialize($config);//初始化分类页
        $data['page'] = $this->pagination->create_links(); //输出的分页结构
        //分页结束
        if(!empty($_GET['per_page'])){
            $offset = $_GET['per_page']?($_GET['per_page']-1)* $limit:1;
        }else{
            $offset = $this->uri->segment(3)?$this->uri->segment(3):0;
        }
        $data['orders'] = $this->orders_model->list_user_order($limit,$offset,$user_id,$pay_status);
        $this->load->view('member/center.html',$data);

    }

    //用户中心ajax请求信息
    /*public function center_ajax(){
        //看session是否存在当前用户user_id，不存在就让其登录
        //当前用户user_id
        $user=$this->session->userdata('user');
        if(!empty($user)){
            $user_id = $user['user_id'];
            $pay_status = $this->input->get("status");
            //CI框架自带分页开始
            $config['base_url'] = base_url()."user/center?status={$pay_status}";
            //每页显示的条数
            $config['per_page']=$limit=1;
            //放在你当前页码的前面和后面的“数字”链接的数量。比方说值为 2 就会在每一边放置 2 个数字链接，就像此页顶端的示例链接那样。
            $config['num_links'] = 4;
            $config['uri_segment'] = 3;
            $config['cur_tag_open'] = '<li class="am-active"><a href="#">';
            $config['cur_tag_close'] = '</a></li>';
            $config['prev_tag_open'] = '<li>';
            $config['prev_tag_close'] = '</li>';
            $config['next_tag_open'] = '<li>';
            $config['next_tag_close'] = '</li>';
            $config['num_tag_open'] = '<li>';
            $config['num_tag_close'] =  '</li>';
            $config['use_page_numbers'] = true;//默认分页URL中是显示每页记录数,启用use_page_numbers后显示的是当前页码，
            $config['first_link'] = '首页'; //你希望在分页的左边显示“第一页”链你不希接的名字。如果望显示，可以把它的值设为 FALSE 。
            $config['last_link'] = '尾页';//你希望在分页的右边显示“最后一页”链接的名字。如果你不希望显示，可以把它的值设为 FALSE 。
            $config['prev_link'] = '上一页';
            $config['next_link'] = '下一页';
            $config['page_query_string'] = true;
            //分页的总条数
            $config['total_rows']=$this->orders_model->count_user_order($user_id,$pay_status);
            $this->pagination->initialize($config);//初始化分类页
            $data['page'] = @str_replace('href','ajaxhref',$this->pagination->create_links()); //输出的分页结构
            //分页结束
            if(!empty($_GET['per_page'])){
                $offset = $_GET['per_page']?($_GET['per_page']-1)* $limit:1;
            }else{
                $offset = $this->uri->segment(3)?$this->uri->segment(3):0;
            }
            $data['orders'] = $this->orders_model->list_user_order($limit,$offset,$user_id,$pay_status);

        }
        $this->load->view('member/center_ajax.html',$data);
    }*/

    //用户中心修改密码
    public function updpass(){
        $this->load->view('member/modifyPwd.html');
    }

	#注销动作
	public function logout(){
		$this->session->unset_userdata('user');
		redirect('home/index');
	}
}